package com.nbsaas.boot;

import javax.script.*;
import java.util.Arrays;

public class CachedKotlinScript {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager(CachedKotlinScript.class.getClassLoader());
        ScriptEngine engine = manager.getEngineByName("kotlin");

        //ScriptEngine engine = new KotlinJsr223JvmLocalScriptEngineFactory().getScriptEngine();
        Compilable compilable = (Compilable) engine;

        // 预编译脚本
        String script = "val total = numbers.sum(); println(total);total";
        CompiledScript compiledScript = compilable.compile(script);

        for (int i = 0; i < 3; i++) {
            Bindings bindings = engine.createBindings();
            bindings.put("numbers", Arrays.asList(i, i*2, i*3));
            Object result = compiledScript.eval(bindings);
            System.out.println("Result: " + result);
        }
    }
}
